package com.qianfeng.qlm.handforireader.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.lidroid.xutils.BitmapUtils;
import com.qianfeng.qlm.handforireader.R;
import com.qianfeng.qlm.handforireader.model.CategoryDetail;
import com.qianfeng.qlm.handforireader.util.Constant;

import org.w3c.dom.Text;

import java.util.List;

/**
 * Created by jiangfeng on 2015/3/25.
 */
public class CategoryDetailAdapter extends BaseAdapter {

    private Context context;
    private List<CategoryDetail> data;
    private RequestQueue mQueue;

    private Handler handler;

    public CategoryDetailAdapter(Context context, List<CategoryDetail> data,RequestQueue mQueue) {
        this.context = context;
        this.data = data;
        this.mQueue = mQueue;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_category_detail,parent,false);
        }

        ViewHolder holder = (ViewHolder) convertView.getTag();
        if (holder == null){
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.item_category_detail_image);
            holder.name = (TextView) convertView.findViewById(R.id.item_category_detail_name);
            holder.author = (TextView) convertView.findViewById(R.id.item_category_detail_author);
            holder.zuixin = (TextView) convertView.findViewById(R.id.item_category_detail_new);
            holder.des = (TextView) convertView.findViewById(R.id.item_category_detail_des);
            convertView.setTag(holder);
        }

        if (position % 2 == 1){
            convertView.setBackgroundColor(Color.argb(33,88,88,88));
        }else {
            convertView.setBackgroundColor(Color.TRANSPARENT);
        }

        CategoryDetail categoryDetail = data.get(position);
        holder.name.setText(categoryDetail.getComic_name());
        holder.author.setText(categoryDetail.getPainter_user_nickname());
        holder.zuixin.setText("更新至" + categoryDetail.getComic_last_orderidx() + "话");
        holder.des.setText(categoryDetail.getComic_desc());

        String url = Constant.URL_SUFFIX + categoryDetail.getComic_pic_240_320();

        final ImageView handlerImageView = holder.imageView;

        BitmapUtils bitmapUtils = new BitmapUtils(context);
        bitmapUtils.configDefaultLoadingImage(R.mipmap.homebg);
        bitmapUtils.display(handlerImageView,url);

//        ImageRequest imageRequest = new ImageRequest(
//                url,
//                new Response.Listener<Bitmap>() {
//                    @Override
//                    public void onResponse(Bitmap response) {
//                        handlerImageView.setImageBitmap(response);
//                    }
//                },
//                200,400,Bitmap.Config.ARGB_8888,
//                new Response.ErrorListener() {
//                    @Override
//                    public void onErrorResponse(VolleyError error) {
//
//                    }
//                }
//        );
//        mQueue.add(imageRequest);

        return convertView;
    }

    public class ViewHolder{
        private ImageView imageView;
        private TextView name;
        private TextView author;
        private TextView zuixin;
        private TextView des;
    }

}
